/**
 * @author Kristian Mandrup
 * @date May 14, 2009
 * @version 0.8
 * 
 * jsTree.generator :: person select functions for person ruleSet in generatorRuleset
 * 
 * - Functions to set the values of a jsTree node object
 */

var personSelectFunctions = {
	selectLanguages: function(person, varPool){
		varPool.eventController.handle('onBeginSelectLanguages', varPool);
		var languages = {
			en: {},
			dk: {}
		};
		return languages;
	},	
	
	selectIcon : function(person, varPool){
		varPool.eventController.handle('onBeginSelectIcon', varPool);
		var iconsPath = varPool.iconsPath || '';
		var nodeType = varPool.nodeType || '';
		var prefix = '/person/'
		// language specific persons!
		if (varPool.language) {
			prefix += (varPool.language + '/');
		}
		if (nodeType === jsTree.constants.nodeType.REFERENCE) {	
			prefix = 'ref_';
		}
	  	// if person has children choose folder icon with head
	  	if (person.children && person.children.length >0)
	    	return 'person/' + prefix + 'parent.png';
	  	// if person has no children, choose head icon (male/female)
	  	if (person.gender === 'male')
	    	return iconsPath + prefix + 'user_green.png';
	  	else
	    	return iconsPath + prefix + 'user_orange.png';
	},
	
	selectDataAttributes: function (person, varPool) {
		varPool.eventController.handle('onBeginSelectDataAttributes', varPool);
	},
	
	selectTitle : function (person, varPool) {
		varPool.eventController.handle('onBeginSelectTitle', varPool);
	  	// if person has children
	  	if (person.myChildren) {
			var boys = person.myChildren.boys || 0;
			var girls = person.myChildren.girls || 0;
			if (boys.length + girls.length > 0) {
		    	return 'Parent ' + person.name.first + ' children: ' + person.children.length;			
			}
		}
	  	// if person has no children, choose head icon (male/female)
	  	if (person.gender === 'male')
	    	return 'Male ' + person.name.first;
	  	else
	    	return 'Female ' + person.name.first;
	},
		
	selectId : function(person, varPool) {
		varPool.eventController.handle('onBeginSelectId', varPool);		
		var counter = varPool.counter || '';
		var id = "person_" + person.name.first + "_" + person.name.last;
		var uniqueId = id + '__' + counter;
		return {id: id, uniqueId: uniqueId};
	},
	
	selectState : function(person, varPool) {
		varPool.eventController.handle('onBeginSelectState', varPool);		
		var oc = jsTree.plugins.util.oc;				
		var openList = varPool.openList || [];
		var id = personSelectFunctions.selectId(person, varPool);
		if (id.id in oc(openList))
			return "open";
		else return varPool.defaultState || "closed";
	},
	
	selectFriends : function(person, varPool){
		if (person.age > 18)
			return person.friends;
	},
	
	boy18Selector : function(person, varPool) {
		var children = person.myChildren || false;
		var result = [];
		if (children) {
			var boys = person.myChildren.boys || false;
			if (boys) {
				for each (var boy in boys){
					if (boy.age > 17)
						result.push(boy);
				}
			}
		}
		return result;
	}		
}


